//
//  NewFeatureCollectionViewCell.swift
//  Weibo
//
//  Created by 张远文 on 16/4/27.
//  Copyright © 2016年 张远文. All rights reserved.
//

import UIKit
import SnapKit

class NewFeatureCollectionViewCell: UICollectionViewCell {
    
    //MARK: - 显示图片的index
    var index: Int = 0 {
        
        didSet {
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
        }
    }
    
    //开始动画效果
    func startAnimation() {
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { 
            
            self.startButton.hidden = false
            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            
            }) { (_) in
                print("OK")
        }
    }
    
    //MARK: - 按钮监听方法
    @objc private func start() {
        
        print("用我吧")
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewController, object: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //添加子视图
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        
        //设置约束
        //edges 表示相对于四周约束
        iconImage.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        startButton.snp_makeConstraints { (make) in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        
        //设置点击事件
        startButton.addTarget(self, action: #selector(start), forControlEvents: .TouchUpInside)
    }
    
    //MARK: - 懒加载所有子控件
    private lazy var iconImage: UIImageView = UIImageView()
    
    private lazy var startButton: UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", color: nil)
}
